package com.spring.schedule_system.controller;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.schedule_system.dao.ScheduleDao;
import com.spring.schedule_system.dao.Tb_classDao;
import com.spring.schedule_system.entity.Schedule;
import com.spring.schedule_system.entity.Tb_class;
import com.spring.schedule_system.entity.Teacher;
import com.spring.schedule_system.entity.User;
import com.spring.schedule_system.utils.JwtToken;
import com.spring.schedule_system.utils.MyUtil;
import com.spring.schedule_system.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/schedule")
@Api(value = "ScheduleController", description = "排课表相关api")
public class ScheduleController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired
    private ScheduleDao scheduleDao;

//    /**
//     * 添加排课表
//     * @param class_name
//     * @param classroom_name
//     * @param course_name
//     * @param teacher_name
//     * @param time
//     * @param week
//     * @param Authorization
//     * @return
//     * @throws IOException
//     * @throws TokenExpiredException
//     */
//    @ApiOperation("添加排课表")
//    @PostMapping("/web/addSchedule")
//    @ResponseBody
//    public ResponseEntity addSchedule(@RequestParam(required = true) String class_name,
//                                   @RequestParam(required = true) String classroom_name,
//                                   @RequestParam(required = true) String course_name,
//                                   @RequestParam(required = true) String teacher_name,
//                                   @RequestParam(required = true) Date time,
//                                   @RequestParam(required = true) String week,
//                                   @RequestHeader String Authorization) throws IOException, TokenExpiredException {
//        ResponseEntity responseEntity;
//        String token = Authorization;
//        //判断token是否存在或过期
//        if (flag = myUtil.judgeToken(token)){
//            responseEntity = ResponseEntity.forbidden();
//            responseEntity.putDataValue("msg","token不存在或已过期");
//            return responseEntity;
//        }
//        User user = JwtToken.unsign(token, User.class);
//        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户
//        if (user.getUser_role()!=2){
//            responseEntity = ResponseEntity.unauthorized();
//            responseEntity.putDataValue("msg","权限错误");
//            responseEntity.putDataValue("timestamp",myUtil.getTime());
//            return responseEntity;
//        }
//        //根据user_jobNumber查询数据库是否有该用户存在，不存在则注册，存在则注册失败
//        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("teacher_name", teacher_name);
//        Schedule schedule= scheduleDao.selectOne(queryWrapper);
//        //User userFromBase = userDao.selectById(user_name);
//        if (schedule == null){
//            responseEntity = ResponseEntity.ok();
//            Tb_class tb_class1 = new Tb_class();
//            tb_class1.setClass_code(class_code);
//            tb_class1.setClass_count(class_count);
//            tb_class1.setClass_name(class_name);
//            tb_class1.setClass_profession(class_profession);
//            tb_class1.setGrade(grade);
//            tb_classDao.insert(tb_class1);
//            responseEntity.putDataValue("class_id",tb_class1.getClass_id());
//            responseEntity.putDataValue("msg","班级注册成功");
//        }else {
//            responseEntity = ResponseEntity.badRequest();
//            responseEntity.putDataValue("msg","班级注册失败");
//        }
//        return responseEntity;
//    }
//
//    /**
//     * 修改班级信息
//     * @param class_id
//     * @param class_code
//     * @param class_name
//     * @param class_profession
//     * @param class_count
//     * @param grade
//     * @param Authorization
//     * @return
//     * @throws IOException
//     */
//    @ApiOperation("修改班级信息")
//    @PutMapping("/web/updateClass")
//    @ResponseBody
//    public ResponseEntity updateClass(@RequestParam(required = true) int class_id,
//                                      @RequestParam(required = false) String class_code,
//                                      @RequestParam(required = false) String class_name,
//                                      @RequestParam(required = false) String class_profession,
//                                      @RequestParam(required = false) String class_count,
//                                      @RequestParam(required = false) String grade, @RequestHeader String Authorization) throws IOException {
//        ResponseEntity responseEntity;
//        String token = Authorization;
//        if (flag =myUtil.judgeToken(token)) {
//            responseEntity = ResponseEntity.forbidden();
//            responseEntity.putDataValue("msg", "token不存在或已过期");
//            return responseEntity;
//        }
//        User users = JwtToken.unsign(token, User.class);
//        if (users == null) {
//            responseEntity = ResponseEntity.notFound();
//            responseEntity.putDataValue("msg", "用户不存在");
//            return responseEntity;
//        } else if (users.getUser_role()!=2) {
//            responseEntity = ResponseEntity.unauthorized();
//            responseEntity.putDataValue("msg", "没有权限");
//            return responseEntity;
//
//        } else {
//            responseEntity = ResponseEntity.ok();
//            Tb_class tb_class = tb_classDao.selectById(class_id);
//            tb_class.setGrade(grade);
//            tb_class.setClass_profession(class_profession);
//            tb_class.setClass_name(class_name);
//            tb_class.setClass_count(class_count);
//            tb_class.setClass_code(class_code);
//            tb_classDao.updateById(tb_class);
//            responseEntity.putDataValue("msg", "班级修改成功");
//            return responseEntity;
//        }
//    }

    /**
     * 根据schedule_id查询排课表
     * @param schedule_id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据schedule_id查询排课表")
    @PutMapping("/web/selectByClassId")
    @ResponseBody
    public ResponseEntity selectByClassId(@RequestParam(required = true) Integer schedule_id, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        User users = JwtToken.unsign(token, User.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getUser_role()==2) {
            responseEntity=ResponseEntity.ok();
            Schedule schedule = scheduleDao.selectById(schedule_id);
            responseEntity.putDataValue("schedule",schedule);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 根据schedule_id删除排课表信息
     * @param schedule_id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据schedule_id删除排课表信息")
    @PutMapping("/web/deleteSchedule")
    @ResponseBody
    public ResponseEntity deleteSchedule(@RequestParam(required = true) Integer schedule_id,@RequestHeader String Authorization)throws IOException{
        ResponseEntity responseEntity;
        String token=Authorization;
        if(flag=myUtil.judgeToken(token)){
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        User users=JwtToken.unsign(token,User.class);
        if(users==null){
            responseEntity=ResponseEntity.notFound();
            responseEntity.putDataValue("msg","用户不存在");
            return responseEntity;
        }else if(users.getUser_role()==2) {
            Schedule schedule = scheduleDao.selectById(schedule_id);
            if(schedule!=null){
                int is_delete = 1;
                schedule.setIs_delete(is_delete);
                scheduleDao.updateById(schedule);
                responseEntity=ResponseEntity.ok();
                responseEntity.putDataValue("msg","删除成功");
                return responseEntity;
            }
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 排课列表
     * @param Authorization
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation("排课列表")
    @PostMapping("/web/schedulesList")
    @ResponseBody
    public ResponseEntity schedulesList(@RequestHeader String Authorization,
                                       HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        /*星号表示所有的域都可以接受，*/
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");

        ResponseEntity responseData;
        String token = Authorization;
        //通过token解密出来的user对象
        User user = JwtToken.unsign(token, User.class);
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseData = ResponseEntity.forbidden();
            responseData.putDataValue("msg","token不存在或已过期");
            return responseData;
        }
        if (user.getUser_role()!=2){
            responseData = ResponseEntity.badRequest();
            responseData.putDataValue("msg","权限错误");
            responseData.putDataValue("timestamp",myUtil.getTime());
            return responseData;
        }
        QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
        List<Schedule> list = scheduleDao.selectList(wrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("Schedule",list);
        responseData.putDataValue("msg","成功");
        return responseData;
    }
    /**
     * 根据班级查询排课表
     * @param class_name
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据班级查询排课表")
    @PutMapping("/web/selectByClassName")
    @ResponseBody
    public ResponseEntity selectByClassName(@RequestParam(required = true) String class_name, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        User users = JwtToken.unsign(token, User.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getUser_role()==2) {
            responseEntity=ResponseEntity.ok();
            QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("class_name", class_name);
            List<Schedule> schedule = scheduleDao.selectList(queryWrapper);
            responseEntity.putDataValue("schedule",schedule);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }
    /**
     * 根据教师查询排课表
     * @param teacher_name
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据教师查询排课表")
    @PutMapping("/web/selectByTeacherName")
    @ResponseBody
    public ResponseEntity selectByTeacherName(@RequestParam(required = true) String teacher_name, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        User users = JwtToken.unsign(token, User.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getUser_role()==2) {
            responseEntity=ResponseEntity.ok();
            QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("teacher_name", teacher_name);
            List<Schedule> schedule = scheduleDao.selectList(queryWrapper);
            responseEntity.putDataValue("schedule",schedule);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }
    /**
     * 根据教室查询排课表
     * @param classroom_name
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据教室查询排课表")
    @PutMapping("/web/selectByClassroomName")
    @ResponseBody
    public ResponseEntity selectByClassroomName(@RequestParam(required = true) String classroom_name, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        User users = JwtToken.unsign(token, User.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getUser_role()==2) {
            responseEntity=ResponseEntity.ok();
            QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("classroom_name", classroom_name);
            List<Schedule> schedule = scheduleDao.selectList(queryWrapper);
            responseEntity.putDataValue("schedule",schedule);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }
    /**
     * 根据时间段查询排课表
     * @param time
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据时间段查询排课表")
    @PutMapping("/web/selectByTime")
    @ResponseBody
    public ResponseEntity selectByTime(@RequestParam(required = true) String time, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        User users = JwtToken.unsign(token, User.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getUser_role()==2) {
            responseEntity=ResponseEntity.ok();
            QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("time", time);
            List<Schedule> schedule = scheduleDao.selectList(queryWrapper);
            responseEntity.putDataValue("schedule",schedule);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }


    /**
     * 统计排课表数量
     */
    @ApiOperation("统计排课表数量")
    @GetMapping("/web/count")
    @ResponseBody
    public ResponseEntity count() throws IOException {
        ResponseEntity responseData;
        int is_delete = 0;
        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", is_delete);
        int  count =scheduleDao.selectCount(queryWrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("count", count);
        return responseData;
    }
    /**
     * 批量删除排课表
     * @param arr
     * @param Authorization
     * @return
     */
    @ApiOperation("批量删除排课表")
    @GetMapping("/web/deleteScheduleList")
    @ResponseBody
    public ResponseEntity deleteScheduleList(@RequestParam(required = true) int[] arr,
                                          @RequestHeader String Authorization ) throws  IOException{
        ResponseEntity responseData;
        String token = Authorization;
        //通过token解密出来的user对象
        User user = JwtToken.unsign(token, User.class);
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseData = ResponseEntity.forbidden();
            responseData.putDataValue("msg","token不存在或已过期");
            return responseData;
        }
        if (user.getUser_role()!=2){
            responseData = ResponseEntity.badRequest();
            responseData.putDataValue("msg","权限错误");
            responseData.putDataValue("timestamp",myUtil.getTime());
            return responseData;
        }
        for (int schedule_id : arr) {
            Schedule schedule = scheduleDao.selectById(schedule_id);
            int is_delete = 1;
            schedule.setIs_delete(is_delete);
            scheduleDao.updateById(schedule);
        }
        responseData = ResponseEntity.ok();
        responseData.putDataValue("msg", "批量删除数据成功");
        return responseData;


    }
}
